import java.util.ArrayList;
import java.util.List;

//Definition for a binary tree node.
class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

class Solution {
    List<String> ret = new ArrayList<>();
    public List<String> binaryTreePaths(TreeNode root) {
        dfs(root, new StringBuilder());
        return ret;
    }
    public void dfs(TreeNode root, StringBuilder s){
        if(root == null) return;
        StringBuilder ss = new StringBuilder(s);
        if(root.left == null && root.right == null) {
            ss.append(root.val);
            ret.add(ss.toString());
            return;
        }
        ss.append(root.val);
        ss.append("->");
        dfs(root.left, ss);
        dfs(root.right, ss);
    }
}